#include "FastMoveWidget.h"

FastMoveWidget::FastMoveWidget(QWidget *parent) : QWidget(parent)
{
    initContent();
    initConnection();
}

void FastMoveWidget::initConnection()
{
    for(int i = 0; i < 10; i++)
    {
        connect(MoveToButton[i], SIGNAL(clicked(bool)), this, SLOT(on_moveTo()));
        connect(FetchPosButton[i], SIGNAL(clicked(bool)), this, SLOT(on_fetchPos()));
    }

}

void FastMoveWidget::setPos(int i, double x, double y)
{
    XPosLabel[i]->setNum(x);
    YPosLabel[i]->setNum(y);
}

void FastMoveWidget::on_moveTo()
{
    QPushButton* button = static_cast<QPushButton *> (sender());

    for(int i = 0; i < 10; i++)
    {
        if( button == MoveToButton[i] )
        {
            QString strX = XPosLabel[i]->text();
            QString strY = YPosLabel[i]->text();
            double x = strX.toDouble();
            double y = strY.toDouble();

            emit gotoPos(x, y);
        }
    }
}

void FastMoveWidget::on_fetchPos()
{
    QPushButton* button = static_cast<QPushButton *> (sender());

    for(int i = 0; i < 10; i++)
    {
        if( button == FetchPosButton[i] )
        {
            emit fetchPos(i);
        }
    }
}

void FastMoveWidget::initContent()
{
    gridLayout = new QGridLayout(this);
    gridLayout->setSpacing(1);

    for(int i = 0;i < 10; i++)
    {
        numLabel[i] = new QLabel(this);
        numLabel[i]->setNum(i);
        numLabel[i]->setFixedWidth(20);

        XPosLabel[i] = new QLabel(this);
        XPosLabel[i]->setNum(0);
        XPosLabel[i]->setFrameStyle(QFrame::Box | QFrame::Sunken);
        XPosLabel[i]->setMinimumWidth(60);
        //XPosLabel[i]->setFixedWidth(50);

        YPosLabel[i] = new QLabel(this);
        YPosLabel[i]->setNum(0);
        YPosLabel[i]->setFrameStyle(QFrame::Box | QFrame::Sunken);
        YPosLabel[i]->setMinimumWidth(60);
        //YPosLabel[i]->setFixedWidth(50);

        FetchPosButton[i] = new QPushButton(this);
        FetchPosButton[i]->setFixedWidth(50);
        //FetchPosButton[i]->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
        FetchPosButton[i]->setText("Fetch");

        MoveToButton[i] = new QPushButton(this);
        MoveToButton[i]->setFixedWidth(50);
        //MoveToButton[i]->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
        MoveToButton[i]->setText("Goto");

        gridLayout->addWidget(numLabel[i], i, 0, 1, 1);
        gridLayout->addWidget(XPosLabel[i], i, 1, 1, 1);
        gridLayout->addWidget(YPosLabel[i], i, 2, 1, 1);
        gridLayout->addWidget(FetchPosButton[i], i, 3, 1, 1);
        gridLayout->addWidget(MoveToButton[i], i, 4, 1, 1);
    }
}
